<template>
  <div class="wrap">
    <div class="mb60">
      <p class="font18 color333 mb30 bolder">定时触发</p>
      <el-form :model="dataForm" :rules="rules" ref="dataForm" >
        <el-form-item label="触发时间：" prop="triggerTime"  required>
          <el-date-picker
            v-model="dataForm.triggerTime"
            type="datetime"
            placeholder="选择触发时间"
            clearable
            format="yyyy-MM-dd HH:mm:ss"
            value-format="yyyy-MM-dd HH:mm:ss"
            :picker-options="timeCheck"
            style="width:300px"
          ></el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer t_c">
      <el-button class="cancelButton mr90" size="small" @click="handleClose">取消</el-button>
      <el-button class="searchButton" size="small" @click="save">确认</el-button>
    </div>
  </div>
</template>

<script>
// 自动化营销-----等待组件
export default {
  name: "timeTrigger",
  components: {},

  data() {
    return {
      tripStartTime: null,
      tripEndTime: null,
      dataForm: {
        triggerTime: null
      },
      rules: {
        triggerTime: [
          { triggerTime: true, message: "请输入触发时间", trigger: "change" }
        ]
      },
      // 活动时间校验
      timeCheck: {
        disabledDate: time => {
          if (this.tripStartTime) {
            return (
              time.getTime() < new Date(this.tripStartTime).getTime() - 8.64e7 + 1 ||
              time.getTime() > new Date(this.tripEndTime).getTime() - 1
            );
          }else {
            return (
              time.getTime() < new Date().getTime() - 8.64e7
            );
          }
        }
      }
    };
  },

  props: {
    nodeInfo: {
      type: Object
    },
    journeyId: {
      type: String,
      default: ""
    }
  },

  methods: {
    save() {
      //保存数据
      let this_ = this;
      //校验
      if (this_.dataForm.triggerTime == null) {
        this_.$message({ message: "请选择时间", type: "error" });
        return;
      }
      if(this_.tripStartTime){
        if(new Date(this_.tripStartTime).getTime() > new Date(this_.dataForm.triggerTime) || new Date(this_.dataForm.triggerTime).getTime() > new Date(this_.tripEndTime).getTime()){
          this_.$message({ message: "时间必须在旅程时间内", type: "error" });
          return;
        }
      }else {
        if(!(new Date(this_.dataForm.triggerTime).getTime() >= new Date().getTime())){
          this_.$message({ message: "时间必须大于等于当前时间", type: "error" });
          return;
        }
      }
      let params = {};
      params.id = this.nodeInfo.nodeId;
      params.nodeVariable = JSON.stringify(this_.dataForm);
      this_.utils.request.updateBranchNode(params, function(res) {
        if (res.code == "0000") {
          this_.saveDlg = false;
          this_.handleClose();
          this_.saveLabelData();
          this_.$message({ message: "操作成功", type: "success" });
        } else {
          this_.$message({ message: "操作失败, " + res.msg, type: "error" });
        }
      });
    },
    //子关闭 标签选择弹框
    handleClose() {
      this.$emit("handleClose");
    },
    saveLabelData(){
      let nodeName = null;
      if(this.dataForm.triggerTime != null){
        nodeName = this.dataForm.triggerTime;
      }
      this.$emit("saveLabelData", {nodeName: nodeName});
    },
    //初始化
    getBranchNodeDetail: function() {
      let this_ = this;
      let param = {};
      param.branchNodeId = this_.nodeInfo.nodeId;
      this.utils.request.getBranchNodeDetail(param, function(res) {
        if (res.code == "0000") {
          if (res.data.nodeVariable != null) {
            JSON.parse(res.data.nodeVariable, function(key, value) {
              if (key == "triggerTime" && value != null) {
                this_.dataForm.triggerTime = value;
              }
            });
          }
        } else {
        }
      });
    },

    //获取旅程详情
    getTripDetailAndNodeList() {
      let this_ = this;
      let callback = function(res) {
        if (res.code == "0000") {
          //固定时间
          if (res.data.timeType == 1) {
            this_.tripStartTime = res.data.startTime;
            this_.tripEndTime = res.data.endTime;
          }
        }
      };
      let param = { tripId: this.journeyId };
      this.utils.request.getTripDetailAndNodeList(param, callback);
    }
  },
  created() {

  },
  mounted() {
    this.getBranchNodeDetail();
    this.getTripDetailAndNodeList();
  }
};
</script>
   <style lang="scss" scoped>
   .font18{
     font-size: 18px !important;
   }
   .bolder{
     font-weight: 500;
   }
   /deep/ .el-input__icon{
     line-height: 1;
   }
   .mr90{
     margin-right: 90px;
   }
   </style>

